package com.junzhiit.nengji.api.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bainaeco.framework.base.spring.Context;
import com.bainaeco.framework.util.serializer.Format;
import com.junzhiit.nengji.api.service.OrdersGoodsService;
import com.junzhiit.nengji.api.web.exception.OrdersGoodsNotFoundException;
import com.junzhiit.nengji.api.web.vo.OrdersGoodsVO;
import com.junzhiit.nengji.api.web.vo.ResultVO;
import com.junzhiit.nengji.bo.OrdersGoodsBO;
import com.junzhiit.nengji.bo.OrdersGoodsInfoBO;
import com.junzhiit.nengji.dao.GoodsDAO;
import com.junzhiit.nengji.dao.OrdersDAO;
import com.junzhiit.nengji.dao.OrdersGoodsDAO;
import com.junzhiit.nengji.model.Cart;
import com.junzhiit.nengji.model.Orders;
import com.junzhiit.nengji.model.OrdersGoods;
import com.junzhiit.nengji.model.enums.Is;
import com.junzhiit.nengji.model.enums.OrderGoodsStatus;

@Service
public class OrdersGoodsServiceImpl extends Context implements OrdersGoodsService {

	@Autowired
	private OrdersGoodsDAO ordersGoodsDAO;
	
	@Autowired
	private GoodsDAO goodsDAO;
	
	@Autowired
	private OrdersDAO ordersDAO;
	
	public List<Cart> add(String goods, String orderId,String userId,String shopId){
		String[] goodsArray = goods.split(";");
		Double total = new Double(0);
		for(int i = 0;i<goodsArray.length;i++){
			String[] goodsInfo = goodsArray[i].split("_");
//			if(ordinaryGoods!=null){
//				if(ordinaryGoods.contains(goodsInfo[2])){
//					
//				}
//			}
			Double number = Double.parseDouble(goodsInfo[0]);
			Double price = Double.parseDouble(goodsInfo[1]);
			total +=(number*price);
		}
		try{
			List<Cart> cartList = new ArrayList<Cart>();
			for(int i=0; i<goodsArray.length; i++){
				
				String[] goodsInfo = goodsArray[i].split("_");//number_price_goodsId_skuContentId
				OrdersGoodsInfoBO ordersGoodsInfoBO;
				if(goodsInfo.length == 4){
					ordersGoodsInfoBO = goodsDAO.findGoodsIdIsSku(userId,shopId,goodsInfo[3]);
				}else{
					ordersGoodsInfoBO = goodsDAO.findGoodsId(userId,shopId,goodsInfo[2]);
				}
				OrdersGoods orderGoods = new OrdersGoods();
				BeanUtils.copyProperties(ordersGoodsInfoBO, orderGoods);
				orderGoods.setOrderId(orderId);
				orderGoods.setStatus(OrderGoodsStatus.BUY.toIntValue());
				if(ordersGoodsInfoBO.getIsOpenSku() == Is.NO.toIntValue()){
					orderGoods.setPrice(Double.parseDouble(goodsInfo[1]));
					
				}
				
				Orders params = ordersDAO.query(orderId);
				Double money = 0.0;
				if(StringUtils.isNoneBlank(params.getUserCouponsId())){
					if(ordersGoodsInfoBO.getIsOpenSku() == Is.NO.toIntValue()){
						money = Format.decimal((Double.parseDouble(goodsInfo[1]) * Integer.valueOf(goodsInfo[0]))  / total * params.getCouponsMoney());
						orderGoods.setCouponsMoney(money);
					}else{
						money = Format.decimal((ordersGoodsInfoBO.getPrice() * ordersGoodsInfoBO.getNumber()) / total * params.getCouponsMoney());
						orderGoods.setCouponsMoney(money);
					}
					
				}
				
				if(ordersGoodsInfoBO.getIsOpenSku() == Is.NO.toIntValue()){
					orderGoods.setPayMoney(Double.parseDouble(goodsInfo[1]) * Double.parseDouble(goodsInfo[0]) - money);
				}else{
					orderGoods.setPayMoney(ordersGoodsInfoBO.getPrice() * Double.parseDouble(goodsInfo[0]) - money);
				}
				
				orderGoods.setNumber(Integer.parseInt(goodsInfo[0]));
				orderGoods.setProfit(ordersGoodsInfoBO.getProfit());
				ordersGoodsDAO.insert(orderGoods);
				
				//返回信息用于删除购物车的商品
				Cart cart = new Cart();
				cart.setGoodsId(orderGoods.getGoodsId());
				cart.setSkuContentId(orderGoods.getSkuContentId());
				cartList.add(cart);
			}
			return cartList;
		}catch(Exception e){
			logger.error(e.getMessage());
			return null;
		}
	}

	@Override
	public ResultVO getNotEvaluateOrderGoods(String orderId) {
		ResultVO result = new ResultVO();
		List<OrdersGoodsBO> list = ordersGoodsDAO.getNotEvaluateOrderGoods(orderId);
		if(list.size() <= 0){
			throw new OrdersGoodsNotFoundException("订单商品没找到");
		}
		List<OrdersGoodsVO> listVO = new ArrayList<OrdersGoodsVO>();
		for(OrdersGoodsBO ordersGoodsBO : list){
			OrdersGoodsVO ordersGoodsVO = new OrdersGoodsVO();
			BeanUtils.copyProperties(ordersGoodsBO, ordersGoodsVO);
			listVO.add(ordersGoodsVO);
		}
		result.setData(listVO);
		return result;
	}
	
}
